//
//  QJMineOrderFooterTwo.m
//  QuanJieShopProject
//
//  Created by 刘中义 on 2018/3/2.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJMineOrderFooterTwo.h"
#import "QJOrderPayVC.h"
#import "QJPointPayVC.h"
#import "YFLogisticsViewController.h"
@implementation QJMineOrderFooterTwo

-(void)awakeFromNib{
    
    [super awakeFromNib];
    // 取消订单
    self.button1.layer.borderWidth = 1;
    self.button1.layer.borderColor = YFColor_Red.CGColor;
    
    // 删除订单
    self.button2.layer.borderWidth = 1;
    self.button2.layer.borderColor = YFColor_Red.CGColor;
    
    self.button3.layer.borderWidth = 1;
    self.button3.layer.borderColor = YFColor_Red.CGColor;
    
    
}
- (void)footerUpdateModel:(MineOrderModel *)model payStatus:(NSString *)payStatus{
    if (model) {
        _model = model;
        NSInteger amount = 0;
        for (MineOrderDetailModel *detailModel in model.detailArr) {
            NSInteger num = [detailModel.amount integerValue];
            amount = amount + num;
        }
        self.shopNumberLabel.text = [NSString stringWithFormat:@"共%ld件商品,合计", amount];
        self.shopTotalPriceLabel.text = [NSString stringWithFormat:@"￥%@",model.total_amount];
        
        if ([payStatus isEqualToString:@"0"]) {
            
            if ([model.order_status isEqualToString:@"0"]) {//待支付
                self.button1.hidden = NO;
                self.button2.hidden = NO;
                self.button3.hidden = YES;
                [self.button1 setTitle:@"取消订单" forState:UIControlStateNormal];
                self.button1.layer.borderColor = YFColor_Text.CGColor;
                [self.button1 setTitleColor:YFColor_Text forState:UIControlStateNormal];
                [self.button2 setTitle:@"去付款" forState:UIControlStateNormal];
                
            }
            if ([model.order_status isEqualToString:@"3"]) {//代发货  已取消
                
                self.button2.hidden = YES;
                self.button3.hidden = YES;
                self.button1.hidden = NO;
                [self.button1 setTitle:@"已取消" forState:UIControlStateNormal];
                self.button1.layer.borderColor = YFColor_Text.CGColor;
                [self.button1 setTitleColor:YFColor_Text forState:UIControlStateNormal];
                
            }
            if ([model.order_status isEqualToString:@"1"]) {//待发货

              MineOrderDetailModel *detailModel =  [model.detailArr objectAtIndex:0];
                if ([detailModel.order_status_desc isEqualToString:@"待发货"]) {
                    self.button1.hidden = YES;
                    self.button2.hidden = YES;
                    self.button3.hidden = YES;
                }
                if ([detailModel.order_status_desc isEqualToString:@"待收货"]) {
                    self.button1.hidden = NO;
                    self.button2.hidden = NO;
                    self.button3.hidden = YES;
                    [self.button1 setTitle:@"查看物流" forState:UIControlStateNormal];
                    self.button1.layer.borderColor = YFColor_Red.CGColor;
                    [self.button1 setTitleColor:YFColor_Red forState:UIControlStateNormal];
                    [self.button2 setTitle:@"确认收货" forState:UIControlStateNormal];
                }
               
            }
            if ([model.order_status isEqualToString:@"2"]) {
                
                self.button1.hidden = NO;
                self.button2.hidden = YES;
                self.button3.hidden = YES;
                [self.button1 setTitle:@"删除订单" forState:UIControlStateNormal];
                self.button1.layer.borderColor = YFColor_Red.CGColor;
                [self.button1 setTitleColor:YFColor_Red forState:UIControlStateNormal];
                
            }
           
        }
    }
}
- (IBAction)buttonAction:(UIButton *)sender {
    
        if (sender.tag == 1) {
       
            if ([_model.order_status isEqualToString:@"0"]) {
                if (self.button1) {
                    self.cancelOrderBlcokAction(self.model);
                   
                }
            }
            if ([_model.order_status isEqualToString:@"1"]) {
                [self checkLogistics];//查看物流
            }
            if ([_model.order_status isEqualToString:@"2"]) {
                [self deleteOrder];
            }
        
    }
    if (sender.tag == 2) {
        if ([_model.order_status isEqualToString:@"0"])
        {
            if ([_model.order_status isEqualToString:@"0"])
            {
                [self goPayAction];
            }
        }
        if ([_model.order_status isEqualToString:@"1"]) {
            
            [self confirmGoods];//确认收货
        }
    }
    if (sender.tag == 3) {
        
        
        
    }
}
- (void)deleteOrder{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    [dict setObject:_model.order_id forKey:@"order_id"];
    
    [WWNetRequest fetchDeleteOrderWithParameters:dict success:^(id response) {
        
        [MBPHUD showAlertWithTitle:@"操作成功"];
        
        
    } failure:^(NSDictionary *error) {
        
        [MBPHUD showAlertWithTitle:@"操作失败"];
        
        
    }];
}

- (void)confirmGoods{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    [dict setObject:_model.order_id forKey:@"order_id"];
    
    [WWNetRequest fetchConfirmOrderWithParameters:dict success:^(id response) {
        
        [MBPHUD showAlertWithTitle:@"操作成功"];
        
    } failure:^(NSDictionary *error) {
        
    }];
}

- (void)checkLogistics{
    
    YFLogisticsViewController *vc = [[YFLogisticsViewController alloc]init];
    vc.orderID = _model.order_id;
    UIViewController *VC = [self currentViewController];
    [VC.navigationController pushViewController:vc animated:YES];
}
- (void)goPayAction{
    MineOrderDetailModel *detailModel = (MineOrderDetailModel *)[self.model.detailArr objectAtIndex:0];
    if ([detailModel.is_points isEqualToString:@"0"]) {
        
        UIViewController *VC = [self currentViewController];
        QJOrderPayVC *vc = [[QJOrderPayVC alloc] init];
        vc.payOrderStr = self.model.order_id;
        vc.totalAmountStr = self.model.total_amount;
        
        vc.yeAmount = [detailModel.profit objectForKey:@"balance_income"];
        vc.silverAmount = [detailModel.profit objectForKey:@"balance_integral"];
        vc.pointsAmount = [detailModel.profit objectForKey:@"balance_user_integral"];
        if ([self.model.pay_code isEqualToString:@"alipayapp"]||[self.model.pay_code isEqualToString:@"appWeixinPay"]) {
            
            vc.pay_code = self.model.pay_code;
            
        }
        [VC.navigationController pushViewController:vc animated:YES];
        NSLog(@"去付款");
    }
    if ([detailModel.is_points isEqualToString:@"1"]) {
        
        UIViewController *VC = [self currentViewController];
        QJPointPayVC *vc = [[QJPointPayVC alloc] init];
        vc.payOrderStr = self.model.order_id;
        vc.totalAmountStr = self.model.total_amount;
        
        vc.silverAmount = [detailModel.profit objectForKey:@"balance_integral"];
        vc.pointsAmount = [detailModel.profit objectForKey:@"balance_user_integral"];
        if ([self.model.pay_code isEqualToString:@"alipayapp"]||[self.model.pay_code isEqualToString:@"appWeixinPay"]) {
            
            vc.payOrderStr = self.model.pay_code;
            
        }
        [VC.navigationController pushViewController:vc animated:YES];
        NSLog(@"去付款");
    }
}
//当前视图
-(UIViewController *)currentViewController{
    UIViewController *vc;
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[objc_getClass("UIViewController") class]] ) {
            vc=(UIViewController*)nextResponder;
            
            return vc;
        }
    }
    return vc;
}
@end
